# ！ /usr/bin/python3
# -*- coding:utf-8 -*-
# @Author:Peng Cao
# @File: 10img_模板匹配.py
# @Software: PyCharm
import cv2.cv2 as cv
import matplotlib.pyplot as plt
import numpy as np


def img_match():
    img = cv.imread('./data/lena.jpg', 0)
    template = cv.imread('./data/lena_face.jpg', 0)
    res = cv.matchTemplate(image=img, templ=template, method=cv.TM_SQDIFF)
    # res = cv.matchTemplate(image=img, templ=template, method=cv.TM_SQDIFF_NORMED)  # method=1
    # res = cv.matchTemplate(image=img, templ=template, method=cv.TM_CCOEFF)
    # res = cv.matchTemplate(image=img, templ=template, method=cv.TM_CCOEFF_NORMED)
    # res = cv.matchTemplate(image=img, templ=template, method=cv.TM_CCORR)
    # res = cv.matchTemplate(image=img, templ=template, method=cv.TM_CCORR_NORMED)

    min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
    draw_img = img.copy()
    new_img = cv.rectangle(img=draw_img, pt1=min_loc, pt2=(min_loc[0] + template.shape[0], min_loc[1] + template.shape[1]), color=255, thickness=2)
    cv.imshow('img', img)
    cv.imshow('template', template)
    cv.imshow('new_img', new_img)
    cv.waitKey(0)
    cv.destroyAllWindows()


if __name__ == '__main__':
    img_match()
